#  This is part of the Scopia data visualization example collection
#
#
#  Copyright (C) 2008  Scopia Visual Interfaces Systems 
#                      http://www.scopia.es
#
#  author: Alejandro Conty
#  contributors:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
from scopiavf.engine.rendertree import *
from livetree import LiveTree
import random

names = [ 'ello', 'cosa', 'nada', 'mierda', 'foo', 'var', 'queso', 'caca', 'casa', 'home', 'piedra' ]

paths = open( 'access.log', 'rb').read().split('\n')

class LiveTreeNode(RenderGeomNode):

    def __init__(self, context, style):
        RenderGeomNode.__init__(self, context, style)
        self.livetree = LiveTree( 500 )
        self.acctime = 0.0
        self.currentPath = 0
        self.accpaths = 0
        context.addTimer(self.timer)
        self.goDynamic()

    def renderAll(self):
        self.style.drawTree( self.context, self, self.livetree )

    def timer(self, elapsed):
        self.acctime += elapsed
        self.livetree.update(elapsed)
        if self.acctime > 0.2:
            self.acctime = 0.0
            self.addRandomPath()
            self.accpaths += 1
        if self.accpaths > 4:
            self.accpaths = 0
            self.livetree.removeDead()
        return True

    def addRandomPath(self):
        #lenght = random.randint(1, 5)
        #path = [ random.choice( names ) for i in xrange(lenght) ]
        #path = '/' + '/'.join(path)
        path = paths[self.currentPath]
        self.livetree.addPath(path)
        self.currentPath = (self.currentPath + 1) % len(paths)
